package paidaxing.blog.post.demo;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class ThreadPoolTest {

    public static void main(String[] args) throws InterruptedException {
        // 快速创建线程池
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
                2,
                4,
                30,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(10),
                new ThreadFactory() {
                    private final AtomicInteger counter = new AtomicInteger(1);
                    @Override
                    public Thread newThread(Runnable r) {
                        return new Thread(r, "test-thread-" + counter.getAndIncrement());
                    }
                },
                new ThreadPoolExecutor.CallerRunsPolicy()
        );

        System.out.println("=== 简单线程池测试开始 ===");

        // 测试1: 基本功能
        testBasicFunction(threadPool);

        // 测试2: 查看线程池状态
        printStatus(threadPool, "测试前");

        // 测试3: 提交多个任务
        testMultipleTasks(threadPool);

        // 关闭线程池
        threadPool.shutdown();
        threadPool.awaitTermination(5, TimeUnit.SECONDS);
        System.out.println("=== 测试结束 ===");
    }

    private static void testBasicFunction(ThreadPoolExecutor threadPool) {
        System.out.println("--- 测试基本功能 ---");

        // 提交简单任务
        threadPool.execute(() -> {
            System.out.println("Hello from " + Thread.currentThread().getName());
        });

        // 提交有返回值的任务
        Future<String> future = threadPool.submit(() -> {
            Thread.sleep(500);
            return "Result from " + Thread.currentThread().getName();
        });

        try {
            String result = future.get();
            System.out.println("任务结果: " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void testMultipleTasks(ThreadPoolExecutor threadPool) throws InterruptedException {
        System.out.println("--- 测试多个任务 ---");

        int taskCount = 8;
        CountDownLatch latch = new CountDownLatch(taskCount);

        for (int i = 1; i <= taskCount; i++) {
            final int taskId = i;
            threadPool.execute(() -> {
                try {
                    System.out.println("任务 " + taskId + " 在 " + Thread.currentThread().getName() + " 中执行");
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                } finally {
                    latch.countDown();
                }
            });
        }

        // 等待所有任务完成
        latch.await();
        System.out.println("所有任务完成!");
        printStatus(threadPool, "测试后");
    }

    private static void printStatus(ThreadPoolExecutor threadPool, String tag) {
        System.out.println("【" + tag + "】" +
                " 活跃线程: " + threadPool.getActiveCount() +
                "/" + threadPool.getPoolSize() +
                ", 队列: " + threadPool.getQueue().size() +
                ", 完成: " + threadPool.getCompletedTaskCount());
    }
}